//
//  StasticChartsViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/12/13.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit
import Charts
class StasticChartsViewController: BaseTableViewController {
    @IBOutlet weak var orgField: UITextField!
    var selectedCompany = ""
    var companys: [JSON] = []
    @IBOutlet weak var lineView: LineChartView!
    @IBOutlet weak var barView: BarChartView!
    @IBAction func chooseCompanyAction(_ sender: Any) {
        let alert = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
        
        for item in companys {
            let sheet = UIAlertAction.init(title: item.contents?["name"]?.rawString(), style: .default) { (action) in
                
                self.orgField.text = item.contents?["name"]?.rawString()
                
                self.selectedCompany = item.contents?["code"]?.rawString() ?? ""
                
                self.load(with: self.names[1])
            }
            alert.addAction(sheet)
        }
        let cancel = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
        alert.addAction(cancel)
        self.present(alert, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configBarChart()
        configLineChart()
        self.load(with: names[0])
    }
    
    func configLineChart()->Void{
        lineView.backgroundColor = UIColor.white
        lineView.noDataText = "暂无数据"
        lineView.scaleYEnabled = false
        
        lineView.doubleTapToZoomEnabled = false
        
        lineView.dragEnabled = false
        
        lineView.dragDecelerationEnabled = false
        
        lineView.dragDecelerationFrictionCoef = 0.9
        
        lineView.rightAxis.enabled = false
        
        lineView.setExtraOffsets(left: 0, top: 30, right: 0, bottom: 30)
        
        let leftAxis = lineView.leftAxis
        
        leftAxis.axisMinimum = 0.0
        
        leftAxis.labelPosition = .outsideChart
        
        leftAxis.labelCount = 5
        
        leftAxis.axisLineColor = .hex("666666")
        
        leftAxis.gridAntialiasEnabled = true
        
        leftAxis.valueFormatter = SymbolsValueFormatter()
        
        let xAxis = lineView.xAxis
        
        xAxis.labelPosition = .bottom
        
        xAxis.drawGridLinesEnabled = false
        
        xAxis.axisLineWidth = 1
        
        lineView.maxVisibleCount = 999
        
        lineView.xAxis.valueFormatter = DateValueFormatter()
        
        lineView.legend.enabled = false
        
        lineView.drawMarkers = true
        
        let marker = BarMarker(color: UIColor(white: 0, alpha: 0.7), font: .systemFont(ofSize: 12), textColor: .white, insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
        
        marker.chartView = lineView
        
        marker.minimumSize = CGSize(width: 80, height: 40)
        
        lineView.marker = marker
        
        lineView.setExtraOffsets(left: 0, top: 30, right: 0, bottom: 30)
        
        lineView.chartDescription = Description.init()
        
        lineView.chartDescription!.text = "(万元)"
        
        lineView.chartDescription!.font = UIFont.systemFont(ofSize: 14)
        
        lineView.chartDescription!.position = CGPoint(x: 50, y: 10)
        
        lineView.animate(yAxisDuration: TimeInterval.init(1.0))
        
    }
    
    func configBarChart()->Void{
        barView.backgroundColor = UIColor.white
        
        barView.legend.enabled = false
        
        barView.chartDescription?.enabled = false
        
        barView.noDataText = "暂无数据"
        
        barView.scaleYEnabled = false
        
        barView.doubleTapToZoomEnabled = false
        
        barView.dragEnabled = false
        
        barView.dragDecelerationEnabled = false
        
        barView.dragDecelerationFrictionCoef = 0.9
        
        barView.rightAxis.enabled = false
        
        barView.setExtraOffsets(left: 0, top: 30, right: 0, bottom: 30)
        
        let leftAxis = barView.leftAxis
        
        leftAxis.axisMinimum = 0.0
        
        leftAxis.labelPosition = .outsideChart
        
        leftAxis.labelCount = 5
        
        leftAxis.axisLineColor = .hex("666666")
        
        leftAxis.gridAntialiasEnabled = true
        
        leftAxis.valueFormatter = SymbolsValueFormatter()
        
        let xAxis = barView.xAxis
        
        xAxis.labelPosition = .bottom
        
        xAxis.drawGridLinesEnabled = false
        
        xAxis.axisLineWidth = 1
        
        barView.maxVisibleCount = 999
        
        barView.xAxis.valueFormatter = DateValueFormatter()
        
        barView.legend.enabled = false
        
        barView.drawMarkers = true
        
        let marker = BarMarker(color: UIColor(white: 0, alpha: 0.7), font: .systemFont(ofSize: 12), textColor: .white, insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
        
        marker.chartView = barView
        
        marker.minimumSize = CGSize(width: 80, height: 40)
        
        barView.marker = marker
        
        barView.setExtraOffsets(left: 0, top: 30, right: 0, bottom: 30)
        
        barView.chartDescription = Description.init()
        
        barView.chartDescription!.text = "(万元)"
        
        barView.chartDescription!.font = UIFont.systemFont(ofSize: 14)
        
        barView.chartDescription!.position = CGPoint(x: 50, y: 10)
        
        barView.animate(yAxisDuration: TimeInterval.init(1.0))
    }
    
    override var names: Array<String>{
        get{
            return ["GETORG","Details"]
        }
    }
    
    override func path(for name: String) -> String {
        switch name {
        case names[0]:
            return GETORG
        default:
            return ""
        }
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        switch name {
        case names[0]:
            return [:]
        default:
            return ["orgcode":""]
        }
    }
    
    override func method(for name: String) -> NetMethod {
        return .get
    }
    
    override func success(for name: String) -> NetSuccess {
        func org(json: JSON)->Void{
            if let data = json.contents?["data"]?.array {
                self.companys = data
                if User.isLeader {
                    let item = self.companys.filter({ (json) -> Bool in
                        return json.contents?["code"]?.rawString() == "x"
                    })
                    if item.count > 0 {
                        self.orgField.text = item[0].contents?["name"]?.rawString()
                        self.selectedCompany = item[0].contents?["code"]?.rawString() ?? ""
                        self.load(with: names[1])
                    }
                }
                if self.selectedCompany.count != 0 {
                    for item in self.companys {
                        if item.contents?["code"]?.rawString() == self.selectedCompany {
                            self.orgField.text = item.contents?["name"]?.rawString()
                        }
                    }
                }
                
                let datas = (0 ..< 12).map { (item) -> ChartDataEntry in
                    return ChartDataEntry.init(x: Double(item), y: Double(item))
                }
                let set = LineChartDataSet.init(values: datas, label: "测试")
                set.colors = (0 ..< 12).map({ (item) -> UIColor in
                    return UIColor.red
                })
                let data = LineChartData.init(dataSets: [set])
                lineView.data = data
            }
        }
        
        func detail(json: JSON)->Void{
            
        }
        
        switch name {
        case names[0]:
            return org
        default:
            return detail
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }
}
